package com.hty.filemanager;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Utils {

	static void writeFile(String filename, String s, int mode) {
		try {
            FileOutputStream FOS = MainApplication.getContext().openFileOutput(filename, mode);
            FOS.write(s.getBytes());
            FOS.close();
		} catch (Exception e) {
            Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
		}
	}

    static String readFile(String filename) {
        String s = "";
        File file = MainApplication.getContext().getFileStreamPath(filename);
        if (file.exists()) {
            try {
                FileInputStream FIS = MainApplication.getContext().openFileInput(filename);
                byte[] buffer = new byte[1024];
                ByteArrayOutputStream BAOS = new ByteArrayOutputStream();
                int len;
                while ((len = FIS.read(buffer)) != -1) {
                    BAOS.write(buffer, 0, len);
                }
                s = new String(BAOS.toByteArray());
                FIS.close();
                BAOS.close();
            } catch (Exception e) {
                Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
            }
        }
        return s;
    }

}